package gateway.rule.filter;

import gateway.api.annotation.GatewayType;
import gateway.api.annotation.Named;
import gateway.api.engine.Chain;
import gateway.api.engine.Filter;
import gateway.api.message.Context;
import gateway.api.message.HttpMessage;
import gateway.api.message.Message;

import java.util.Map;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-23
 */
@Named(value = "pathToContextFilter", type = GatewayType.HTTP, async = false)
public class PathToContextFilter implements Filter
{
	private Map<String, String> args;

	public PathToContextFilter()
	{
	}

	@Override
	public void handle(Message message, Chain chain)
	{
		if (message instanceof HttpMessage)
		{
			HttpMessage httpMessage = (HttpMessage) message;
			Context context = httpMessage.context();
			for (Map.Entry<String, String> entry : httpMessage.pathMap().entrySet())
			{
				context.putArg(entry.getKey(), entry.getValue());
			}
		}
		chain.doNext();
	}

	@Override
	public void setArgs(Map<String, String> args)
	{
		this.args = args;
	}
}
